using System;
using System.Collections.Generic;
using System.Windows.Forms;
using smartBrowser.GUI;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using smartBrowser.Util;
using XPExplorerBar;
using System.Threading;
using smartBrowser.Ontology;
using smartBrowser.Filter;
using System.Drawing;

namespace smartBrowser.Task
{
    class GoogleTask : Task
    {
        public GoogleTask(WebSite webSite, TabPageForm form)
            : base(webSite, form)
        {
        }

        protected override void Execute()
        {
            ResultsFilter filter = new ResultsFilter(this.webSite);
           
            List<Thread> threads = new List<Thread>();

            foreach (Ontology.Ontology ontology in this.webSite.OntologyList)
            {
                foreach (String key in ontology.Ontologia.Keys)
                {
                    SearchObject search_object = new SearchObject(key, 3);   
                    Thread thread = new Thread(new ParameterizedThreadStart(FillExpando));
                    thread.Start(search_object.searchResults());                    
                    threads.Add(thread);
                }
            } 
           

            foreach (Thread thread in threads) {
                thread.Join();
            }

            FireCompleteEvent();
        }

        private void FillExpando(Object results)
        {            
            Expando expando = new Expando();
            expando.Text = "Ontology @Google";
            expando.Name = "Ontology@Google";

            int top = 28;

            foreach (Result result in (List<Result>)results)
            {
                double pageRank = GetPageRank(result.Url) / 2.0;
                XPExplorerBar.TaskItem taskItem = new XPExplorerBar.TaskItem();
                taskItem.Text = result.Title;
                taskItem.Tag = result.Url;
                taskItem.Click += new EventHandler(taskItem_Click);
                taskItem.Location = new System.Drawing.Point(4, top);
                taskItem.Image = global::smartBrowser.Properties.Resources.google;
                
                int i;
                for (i = 1; i <= pageRank; i++)
                {
                    Label label = new Label();
                    label.Image = Image.FromFile(Path.Combine(Application.StartupPath, "star-small.png"));
                    label.Left = i * 12 + 150;
                    label.Top = top;
                    label.Width = 12;
                    label.Height = 12;
                    expando.Items.Add(label);
                }

                if (pageRank - (int)pageRank >= 0.3)
                {
                    Label label = new Label();
                    label.Image = Image.FromFile(Path.Combine(Application.StartupPath, "star-half-small.png"));
                    label.Left = i * 12 + 150;
                    label.Top = top;
                    label.Width = 6;
                    label.Height = 12;
                    expando.Items.Add(label);
                }

                expando.Items.Add(taskItem);
                top += 22;
            }
            expando.Size = new System.Drawing.Size(expando.Size.Width, top);
            AddExpando(expando);
        }

        void taskItem_Click(object sender, EventArgs e)
        {
            SmartBrowser.Instance.OpenNewTabAt(((TaskItem)sender).Tag.ToString());
        }

        private delegate void AddExpandoDelegate(Expando expando);
        private void AddExpando(Expando expando)
        {
            if (this.form.TaskPane.InvokeRequired)
            {
                AddExpandoDelegate del = new AddExpandoDelegate(AddExpando);
                this.form.TaskPane.Invoke(del, new object[] { expando });
            }
            else
            {
                foreach (Expando exp in this.form.TaskPane.Expandos)
                {
                    if (exp.Name == expando.Name)
                    {
                        this.form.TaskPane.Expandos.Remove(exp);
                        break;
                    }
                }

                if (expando.Items.Count > 0)
                    this.form.TaskPane.Expandos.Add(expando);
            }
        }

        int GetPageRank(string link)
        {
            Regex expression = new Regex(".*imgRank\\[(\\d+)\\].*");

            string pageSource = smartBrowser.Util.WebPageFetcher.FecthPage(new Uri("http://www.checkpagerank.com/jscripts/js_pagerank.php?domain=" + link));
            return Convert.ToInt32(expression.Match(pageSource).Groups[1].Value);
        }
    }
}
